package main

import (
	"flag"
	"fmt"
	"iron_man_comm/src/goroslib"
	"iron_man_comm/src/goroslib/dns"
	"iron_man_comm/src/tools/impl"
	"iron_man_comm/src/tools/log"
	"os"
	"time"
)

const TYPE_DESC = "检查类型:\n\t1:帧率\n\t2:数据"
const SENSOR_DESC = "传感器类型:\n\t" +
	"1:lds\n\t2:沿边线激光\n\t3:Odom\n\t4:Imu\n\t5:电机转速\n\t6:rgb原始图像\n\t7:rgb重采样图像\n\t8:红外图像\n\t" +
	"9:基站外设安装状态\n\t10:基站任务状态\n\t11:电池信息\n\t12:HMI地图"

func createRosNode(ip string) (*goroslib.Node, error) {
	dns.GetDNSCacheInterface().UpdateDNSMapping("rk3562-buildroot", ip)
	node, err := goroslib.NewNode(goroslib.NodeConf{
		Name:          fmt.Sprintf("check_tool_%d", time.Now().UnixMilli()),
		MasterAddress: fmt.Sprintf("%s:11311", ip),
	})
	if err != nil {
		return nil, err
	}
	return node, nil
}

const VERSION = "1.4"

func main() {
	log.InitializeLog()

	checkType := flag.Int("t", 0, TYPE_DESC)
	sensorType := flag.Int("s", 0, SENSOR_DESC)
	ip := flag.String("ip", "127.0.0.1", "目标ip")
	hostName := flag.String("n", "rk3562-buildroot", "目标机器hostname")
	flag.Parse()

	if 0 == *checkType || 0 == *sensorType {
		flag.PrintDefaults()
		return
	}

	log.Info.Printf("version: %s, sensor: %d, check type: %d，ip: %s, hostname: %s", VERSION, *sensorType, *checkType, *ip, *hostName)
	if *ip != "127.0.0.1" {
		log.Info.Println("注：跨网络订阅受网络波动影响，帧率误差较大")
	}

	sensorCheck := impl.GetSensorCheck(impl.SensorType(*sensorType))
	if nil == sensorCheck {
		return
	}
	defer sensorCheck.Release()

	node, err := createRosNode(*ip)
	if nil != err {
		log.Error.Println("create node fail, err: ", err.Error())
		return
	}
	defer node.Close()

	switch *checkType {
	case impl.CheckTypeHz:
		sensorCheck.Init(node, impl.ModeHz)
		break
	case impl.CheckTypeData:
		sensorCheck.Init(node, impl.ModeData)
		break
	default:
		log.Error.Printf("valid check type")
		return
	}
	err = sensorCheck.CheckData()

	if nil != err {
		log.Error.Println("error: ", err.Error())
		return
	}

	c := make(chan os.Signal, 1)
	_ = <-c
}
